﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace JMcms.Translate
{
    public class TranslateHttper
    {
        public TranslateHttper() { }
        /// <summary>
        /// 跨域获取数据例子如下：
        ///  JavaScriptSerializer js = new JavaScriptSerializer();
        ///  TokenInfo tk = js.Deserialize<TokenInfo>(result);
        /// </summary>
        /// <param name="requestType">请求类型get/post字符串类型</param>
        /// <param name="parameter">请求参数例如：username=admin&password=ids111111&clientVersion=1.1212&clientType=1</param>
        /// <param name="requestUrl">请求地址例如：http://58.215.198.210:5680/external/common/logon</param>
        /// <param name="token">token：权限</param>
        /// <returns></returns>
        public static string GetData(string requestType,string parameter,string requestUrl,string token)
        {
            string result = string.Empty;
            if (requestType == "get")
            {
                result= HttpGet(requestUrl, parameter, token);
            }
            else
            {
                result=HttpPost(requestUrl, parameter, token);
            }
            return result;

            

        }
        #region 公用方法
        public static string HttpGet(string Url, string postDataStr, string token)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.Headers.Add("token", token);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public static string HttpPost(string Url, string postDataStr, string token)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
            request.Headers.Add("token", token);
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(postDataStr);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            return retString;
        }
        #endregion
    }
}
